package cn.iocoder.yudao.module.famous.doctor.service.extraction;

import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.famous.doctor.controller.admin.extraction.vo.*;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.extraction.ExtractionDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.famous.doctor.convert.extraction.ExtractionConvert;
import cn.iocoder.yudao.module.famous.doctor.dal.mysql.extraction.ExtractionMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.famous.doctor.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 开方 Service 实现类
 *
 * @author 档案
 */
@Service
@Validated
public class ExtractionServiceImpl implements ExtractionService {

    @Resource
    private ExtractionMapper extractionMapper;

    @Override
    public Long createExtraction(ExtractionCreateReqVO createReqVO) {
        // 插入
        ExtractionDO extraction = ExtractionConvert.INSTANCE.convert(createReqVO);
        extractionMapper.insert(extraction);
        // 返回
        return extraction.getId();
    }

    @Override
    public void updateExtraction(ExtractionUpdateReqVO updateReqVO) {
        // 校验存在
        validateExtractionExists(updateReqVO.getId());
        // 更新
        ExtractionDO updateObj = ExtractionConvert.INSTANCE.convert(updateReqVO);
        extractionMapper.updateById(updateObj);
    }

    @Override
    public void deleteExtraction(Long id) {
        // 校验存在
        validateExtractionExists(id);
        // 删除
        extractionMapper.deleteById(id);
    }

    private void validateExtractionExists(Long id) {
        if (extractionMapper.selectById(id) == null) {
            throw exception(EXTRACTION_NOT_EXISTS);
        }
    }

    @Override
    public ExtractionDO getExtraction(Long id) {
        return extractionMapper.selectById(id);
    }

    @Override
    public List<ExtractionDO> getExtractionList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return extractionMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ExtractionDO> getExtractionPage(ExtractionPageReqVO pageReqVO) {
        return extractionMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ExtractionDO> getExtractionList(ExtractionExportReqVO exportReqVO) {
        return extractionMapper.selectList(exportReqVO);
    }

    @Override
    public IPage<ExtractionDO> getExtractionIPage(ExtractionPageReqVO pageVO) {
        return extractionMapper.selectIpage(MyBatisUtils.buildPage(pageVO),getLoginUserId(),pageVO);
    }


}
